#!/bin/bash

set -eu

# script path
sd="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

hmsdir="$(dirname -- ${sd})"

scdir="scripts"
sf="${hmsdir}/${scdir}/shared_funcs.sh"

if [ -f "$sf" ]; then
    source "$sf"
else
    echo "Script file not found: $sf"
    echo "Exiting..."
    exit
fi

eigenDir="${hmsdir}/lib/eigen"
echo "Directory for eigen library: ${eigenDir}"
if [ -d "${eigenDir}" ] && [ "$(ls -A ${eigenDir})" ]; then
    echo -e "Directory exists and is not empty:\n    ${eigenDir}\nSkipping building eigen..."
    exit
else
    echo "Downloading and building eigen..."
    echo "Configuration step may take a long time, don't abort!"
fi
git clone https://gitlab.com/libeigen/eigen.git "${eigenDir}"
cd "${eigenDir}"

# last known working commit in 3.3.9
#commitHash=d53ae40f
# tag 3.4.0, that's probably the last stable release
#commitHash=3147391d946bb4b6c68edd901f2add6ac1f31f8c
# latest 3.4 commit doesn't work
#commitHash=e7248b26a1ed53fa030c5c459f7ea095dfd276ac
# latest commit on master (2022-07-05, v. 3.4.90) works
commitHash=06a458a13d7cbea54a2cbf6a491f6bbf1f64c8ce

git checkout "${commitHash}"

mkdir -p build ; cd build
cmake -D CMAKE_INSTALL_PREFIX=../install ..
make install


